package com.xydtech.ecard.config.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.enums.ApiContant;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.data.DataPlatHttpUtils;
import com.xydtech.ecard.config.domain.EcardNewsContentList;
import com.xydtech.ecard.config.domain.NewsContentRequest;
import com.xydtech.ecard.config.mapper.EcardNewsContentListMapper;
import com.xydtech.ecard.config.service.IEcardNewsContentListService;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子名片新闻列Service业务层处理
 *
 * @author JHON
 * @date 2022-04-11
 */
@Service
public class EcardNewsContentListServiceImpl implements IEcardNewsContentListService {
    @Autowired
    private EcardNewsContentListMapper ecardNewsContentListMapper;
    @Autowired
    DataPlatHttpUtils dataPlatHttpUtils;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片新闻列
     *
     * @param id 电子名片新闻列主键
     * @return 电子名片新闻列
     */
    @Override
    public EcardNewsContentList selectEcardNewsContentListById(Long id) {
        return ecardNewsContentListMapper.selectEcardNewsContentListById(id);
    }

    /**
     * 查询电子名片新闻列列表
     *
     * @param ecardNewsContentList 电子名片新闻列
     * @return 电子名片新闻列
     */
    @Override
//    @DataScope(deptAlias = "a")
    public List<EcardNewsContentList> selectEcardNewsContentListList(EcardNewsContentList ecardNewsContentList) {
        return ecardNewsContentListMapper.selectEcardNewsContentListList(ecardNewsContentList);
    }

    /**
     * 新增电子名片新闻列
     *
     * @param ecardNewsContentList 电子名片新闻列
     * @return 结果
     */
    @Override
    public int insertEcardNewsContentList(EcardNewsContentList ecardNewsContentList) {
        ecardNewsContentList.setPubDate(DateUtils.getTime());
        return ecardNewsContentListMapper.insertEcardNewsContentList(ecardNewsContentList);
    }

    /**
     * 下载名片新闻列
     *
     * @param newsContentRequest 接口餐户数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult downLoadNewsContentList(NewsContentRequest newsContentRequest) {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(newsContentRequest.getDeptId());
        //接口编号
        String interfaceNumOcr = configService.selectConfigByKey(ApiContant.DATA_PLAT_NEWS_CONTENT_KEY, dept.getDeptId());
        String channelId = newsContentRequest.getChannelId();
        String[] channelIdArr = channelId.split(",");
        for (int j = 0; j < channelIdArr.length; j++) {
            EcardNewsContentList content = new EcardNewsContentList();
            content.setDeptId(newsContentRequest.getDeptId());
            content.setChannelId(channelIdArr[j]);
            //停用旧的新闻
            ecardNewsContentListMapper.updateEcardNewsContentSts(content);
            //参数
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("channelId", channelIdArr[j]);
            paramMap.put("page", newsContentRequest.getPage());
            paramMap.put("maxResult", newsContentRequest.getMaxResult());

            // 调用数据平台接口
            ResponseModel responseModel = dataPlatHttpUtils.dataPlatRequest(interfaceNumOcr, "", ApiContant.APPLETTYPE_ECARD, newsContentRequest.getDeptId(), paramMap);

            if (responseModel.getCode() != 0) {
                return AjaxResult.error("下载新闻失败:" + responseModel.getMsg());
            }
            // 接口返回结果
            JSONObject responseData = JSON.parseObject(JSON.toJSONString(responseModel.getData()));
            JSONArray contentList = responseData.getJSONArray("contentlist");
            for (int i = 0; i < contentList.size(); i++) {
                EcardNewsContentList newsContent = new EcardNewsContentList();
                newsContent.setChannelId(contentList.getJSONObject(i).getString("channelId"));
                newsContent.setDeptId(newsContentRequest.getDeptId());
                newsContent.setNewsId(contentList.getJSONObject(i).getString("id"));
                newsContent.setLink(contentList.getJSONObject(i).getString("link"));
                if (contentList.getJSONObject(i).getString("content").length() > 100) {
                    newsContent.setSummary(contentList.getJSONObject(i).getString("content").substring(0, 100) + ".......");
                } else {
                    newsContent.setSummary(contentList.getJSONObject(i).getString("content"));
                }
                newsContent.setSource(contentList.getJSONObject(i).getString("source"));
                newsContent.setTitle(contentList.getJSONObject(i).getString("title"));
                newsContent.setPubDate(contentList.getJSONObject(i).getString("pubDate"));
                //目前发现广告图片路径包含fileftp，替换掉不显示广告图片
                newsContent.setHtml(contentList.getJSONObject(i).getString("html").replaceAll("fileftp", "xxxx"));
                newsContent.setImg(contentList.getJSONObject(i).getString("img"));
                newsContent.setIndexSts("F");
                newsContent.setHotSts("F");
                newsContent.setSts("1");
                ecardNewsContentListMapper.insertEcardNewsContentList(newsContent);
            }
        }

        return AjaxResult.success();
    }

    /**
     * 修改电子名片新闻列
     *
     * @param ecardNewsContentList 电子名片新闻列
     * @return 结果
     */
    @Override
    public int updateEcardNewsContentList(EcardNewsContentList ecardNewsContentList) {
        return ecardNewsContentListMapper.updateEcardNewsContentList(ecardNewsContentList);
    }

    /**
     * 批量删除电子名片新闻列
     *
     * @param ids 需要删除的电子名片新闻列主键
     * @return 结果
     */
    @Override
    public int deleteEcardNewsContentListByIds(String ids) {
        return ecardNewsContentListMapper.deleteEcardNewsContentListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片新闻列信息
     *
     * @param id 电子名片新闻列主键
     * @return 结果
     */
    @Override
    public int deleteEcardNewsContentListById(Long id) {
        return ecardNewsContentListMapper.deleteEcardNewsContentListById(id);
    }
}
